package config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import java.nio.charset.StandardCharsets;
import java.util.EnumSet;

/**
 * Created by 张少昆 on 2017/11/6.
 */
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses(){
        return new Class[]{AppConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses(){
        return new Class[]{MvcConfig.class};
    }

    @Override
    protected String[] getServletMappings(){
        System.out.println("映射路径 /");
        return new String[]{"/"};
    }


    @Override
    public void onStartup(ServletContext servletContext) throws ServletException{
        //  *************** 解决编码问题 ***************
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding(StandardCharsets.UTF_8.displayName());
        encodingFilter.setForceEncoding(true);
        FilterRegistration.Dynamic encodingFilterRegistration = servletContext.addFilter("encodingFilter", encodingFilter);
        encodingFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR, DispatcherType.ASYNC), false, "/*");

        //  ***************** 权限认证 *****************
        FilterRegistration.Dynamic securityFilterRegistration = servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy());
        // TODO 注意，这里设置了拦截所有路径
        securityFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR, DispatcherType.ASYNC), false, "/*");

         //  *************** 数据库连接池监控 ***************
         FilterRegistration.Dynamic druidWebStatFilterRegistration = servletContext.addFilter("druidWebStatFilter", new WebStatFilter());
         // 排除一些不必要监控的url
         druidWebStatFilterRegistration.setInitParameter("exclusions", "/,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/monitor.jsp,/monitoring*,/druid/*,/common/*");
         // 缺省sessionStatMaxCount是1000个
         // druidWebStatFilterRegistration.setInitParameter("sessionStatMaxCount", "1000");
         // 关闭session统计功能
         druidWebStatFilterRegistration.setInitParameter("sessionStatEnable", "false");
         // 配置principalSessionName，使得druid能够知道当前的session的用户是谁
         // druidWebStatFilterRegistration.setInitParameter("principalSessionName", "org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY");
         druidWebStatFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR, DispatcherType.ASYNC), false, "/*");

         //  *************** 连接池监控页面 ***************
         ServletRegistration.Dynamic druidStatViewServletRegistration = servletContext.addServlet("statViewServlet", new StatViewServlet());
         // druidStatViewServletServlet.setLoadOnStartup(10);
         druidStatViewServletRegistration.addMapping("/druid/*");

        super.onStartup(servletContext);
    }
}
